package com.dlmz.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;

public class HealAddDialog extends JDialog implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//=========面板控件  
    //......左侧标题栏  
    private JLabel hnoLab,snoLab,heightLab,weightLab,chestLab,bpressureLab,tracmLab,cxLab,pdateLab,presultLab,medicalhistoryLab;  
    //......右侧信息选择填写栏  
    private JTextField hnoTxt,snoTxt,heightTxt,weightTxt,chestTxt,bpressureTxt,tracmTxt,cxTxt,pdateTxt,presultTxt,medicalhistoryTxt;  
    //......添加和取消按钮  
    private JButton addBtn,cancelBtn;  
    //......布局控件  
    private JPanel left,center,bottom;  
      
    //构造函数  
    public HealAddDialog(JFrame owner, String title, boolean modal)   
    {  
        //========重写父类方法  
        super(owner, title, modal);  
        //========左侧标签栏  
        hnoLab = new JLabel("体检单号: ");  
        snoLab = new JLabel("学号: ");  
        heightLab = new JLabel("身高（cm）: ");  
        weightLab = new JLabel("体重(kg): ");  
        chestLab = new JLabel("胸围(cm): ");  
        bpressureLab = new JLabel("血压(mmHg): ");
        tracmLab = new JLabel("沙眼：");
        cxLab=new JLabel("胸透");
        pdateLab=new JLabel("体检日期");
        presultLab=new JLabel("体检结果");
        medicalhistoryLab=new JLabel("病史");
        //========右侧信息填写栏  
        hnoTxt = new JTextField();  
        snoTxt = new JTextField();  
        heightTxt = new JTextField();  
        weightTxt = new JTextField();  
        chestTxt = new JTextField();  
        bpressureTxt = new JTextField();
        tracmTxt = new JTextField();
        cxTxt = new JTextField();  
        pdateTxt = new JTextField();  
        presultTxt = new JTextField();
        medicalhistoryTxt = new JTextField();
        //========添加和取消按钮  
        addBtn = new JButton("添加");  
        cancelBtn = new JButton("取消");  
        //......添加监听  
        addBtn.addActionListener(this);  
        addBtn.setActionCommand("add");  
        cancelBtn.addActionListener(this);  
        cancelBtn.setActionCommand("cancel");  
        //========创建布局  
        //......创建左边栏  
        left = new JPanel();  
        left.setLayout(new GridLayout(11, 1));  
        left.add(hnoLab);  left.add(snoLab);   
        left.add(heightLab); left.add(weightLab);   
        left.add(chestLab);  left.add(bpressureLab); 
        left.add(tracmLab); left.add(cxLab);
        left.add(pdateLab); left.add(presultLab);
        left.add(medicalhistoryLab);
        //......创建右边栏  
        center = new JPanel();  
        center.setLayout(new GridLayout(11, 1));  
        center.add(hnoTxt);  center.add(snoTxt);  
        center.add(heightTxt); center.add(weightTxt);  
        center.add(chestTxt);  center.add(bpressureTxt); 
        center.add(tracmTxt); center.add(cxTxt);  
        center.add(pdateTxt); center.add(presultTxt); 
        center.add(medicalhistoryTxt);
        //========底层添加和取消按钮  
        bottom = new JPanel();  
        bottom.add(addBtn);  
        bottom.add(cancelBtn);  
        //========整体布局  
        this.add(left,BorderLayout.WEST);  
        this.add(center,BorderLayout.CENTER);  
        this.add(bottom,BorderLayout.SOUTH);  
        //========设置窗口属性  
          
        validate();
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		setSize(450,300);
		setVisible(true);   
    }  
  
    public void actionPerformed(ActionEvent e)   
    {  
        // TODO Auto-generated method stub  
        if(e.getActionCommand().equals("add")) {  
            /***********************添加学生信息**************************/   
            String sql = "insert into healthform values(?,?,?,?,?,?,?,?,?,?,?)";  
            String hno =hnoTxt.getText();
            int sno=Integer.parseInt(snoTxt.getText());
            int height=Integer.parseInt(heightTxt.getText());
            float weight=Float.parseFloat(weightTxt.getText());
            float chest=Float.parseFloat(chestTxt.getText());
            String bpressure=bpressureTxt.getText(); 
            String tracmstr=tracmTxt.getText();
            int tracm = 0;
            if(tracmstr=="有") 
            	tracm=1;
            else if(tracmstr=="无")
            	tracm=0;
            String cx=cxTxt.getText();
            String pdate=pdateTxt.getText();
            String presult=presultTxt.getText();
            String medicalhistory=medicalhistoryTxt.getText();
            BaseDao basedao=new BaseDao();
			int num=basedao.update(sql, hno,sno,height,weight,chest,bpressure,tracm,cx,pdate,presult,medicalhistory);
			if(num>0) {
				JOptionPane.showMessageDialog(null, "添加体检信息成功！");
				this.dispose(); 
			}else {
				JOptionPane.showMessageDialog(null, "添加体检信息失败！");
			}
            //========关闭窗口  
            this.dispose();  
        } else if(e.getActionCommand().equals("cancel")) {  
            //========关闭窗口  
            this.dispose();  
        }  
    }

}
